package com.tencent.hummingbird.portal.controller;


import com.tencent.hummingbird.portal.r.R;
import com.tencent.hummingbird.portal.service.IUserCollectService;
import com.tencent.hummingbird.portal.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxb
 * @since 2024-01-10
 */
@Slf4j
@RestController
@RequestMapping("/portal/userCollect")
public class UserCollectController {
    @Autowired
    private IUserService userService;
    @GetMapping("/collect/{id}")
    public R<Integer> collect(@PathVariable Integer id){
        int collects = userService.getCollects(id);
        return R.ok(collects);
    }
    @Autowired
    private IUserCollectService userCollectService;
    @GetMapping("/cancel/{questionId}")
    public R<Integer> cancel(@AuthenticationPrincipal UserDetails userDetails,@PathVariable Integer questionId){
       Integer cancel =  userCollectService.cancelCollect(userDetails.getUsername(),questionId);
       return R.ok(cancel);
    }
    @GetMapping("/make/{questionId}")
    public R make(@AuthenticationPrincipal UserDetails userDetails,@PathVariable Integer questionId){
        log.info("questionId:{}",questionId);
        log.info("userName:{}",userDetails.getUsername());
        userCollectService.addCollect(userDetails.getUsername(),questionId);
        return R.ok("收藏成功");
    }

    @GetMapping("/status/{questionId}")
    public R<Integer> status(@AuthenticationPrincipal UserDetails userDetails,@PathVariable Integer questionId){
        int status = userCollectService.selectCollectStatus(userDetails.getUsername(),questionId);
        log.info("status:{}",status);
        return R.ok(status);
    }
}
